/*
 * @Author: szx
 * @Date: 2022-03-12 13:18:17
 * @LastEditTime: 2022-03-12 13:27:52
 * @Description:
 * @FilePath: \leetcode\1-99\39\39_2.js
 */
/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function (candidates, target) {
    const res = [];
    const backtrace = (path, sum, index) => {
        if (sum > target) return;
        if (sum === target) {
            res.push(Array.from(path));
            return;
        }
        for (let i = index; i < candidates.length; i++) {
            path.push(candidates[i]);
            backtrace(path, sum + candidates[i], i);
            path.pop();
        }
    };
    backtrace([], 0, 0);
    return res;
};
console.log(combinationSum([2, 3, 6, 7], 7));
